package com.bgs.eat.login.service.impl;

import com.bgs.eat.config.CommonResult;
import com.bgs.eat.login.mapper.LoginMapper;
import com.bgs.eat.login.service.LoginService;
import com.bgs.eat.userProtal.entity.UserProtalEntity;
import com.bgs.eat.userProtal.mapper.UserProtalMapper;
import com.bgs.eat.userProtal.service.UserProtalService;
import com.bgs.eat.utils.RanNum;
import com.bgs.eat.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Base64Utils;
import org.springframework.util.DigestUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

/**
 * @ClassName LoginServiceImpl
 * @Description TODO
 * @Author hbx
 * @Date 2022/10/10 20:41
 **/
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    LoginMapper loginMapper;

    @Autowired
    UserProtalMapper userProtalMapper;

    @Autowired
    UserProtalService userProtalService;

    @Autowired
    RedisUtil redisUtil;
    /**
    * @Author hbx
    * @Description 前端用户登录
    * @Date 2022/10/10 20:49
    * @param userName
    * @param password
    */
    @Override
    public CommonResult loginByUser(String userName, String password) {
        UserProtalEntity userProtalEntity = new UserProtalEntity();
        userProtalEntity.setUserName(userName);
        String md5Str = DigestUtils.md5DigestAsHex(password.getBytes());
        userProtalEntity.setPassword(md5Str);
        UserProtalEntity select = userProtalMapper.selectOne(userProtalEntity);
        // 创建时间
        Date createTime = new Date();
        // 时间格式初始化
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        String createTimeSdf = sdf.format(createTime);
        String token = Base64Utils.encodeToString((userProtalEntity.getUserName()+createTimeSdf+userProtalEntity.getPassword()).getBytes());
        if(select != null){
            HashMap<Object, Object> map = new HashMap<>();
            map.put("user", select);
            map.put("token", token);
            return CommonResult.ok(map);
        } else if(select == null){
            int i = userProtalService.addUserProtal(userName,password);
            if( i != 0){
                UserProtalEntity userProtalEntity1 = new UserProtalEntity();
                userProtalEntity1.setUserName(userName);
                String md5Str1 = DigestUtils.md5DigestAsHex(password.getBytes());
                userProtalEntity1.setPassword(md5Str1);
                UserProtalEntity select1 = userProtalMapper.selectOne(userProtalEntity1);
                // 创建时间
                Date createTime1 = new Date();
                // 时间格式初始化
                SimpleDateFormat sdf1 = new SimpleDateFormat("HH:mm:ss");
                String createTimeSdf1 = sdf1.format(createTime);
                String token1 = Base64Utils.encodeToString((userProtalEntity.getUserName()+createTimeSdf1+userProtalEntity.getPassword()).getBytes());
                if( select1 != null){
                    HashMap<Object, Object> map = new HashMap<>();
                    map.put("user", select1);
                    map.put("token", token1);
                    return CommonResult.ok(map);
                }else {
                    return CommonResult.error("999");
                }
            } else {
                return CommonResult.error("999");
            }
        } else {
            return CommonResult.error("999");
        }
    }

    /**
    * @Author hbx
    * @Description 前端根据手机号验证登录
    * @Date 2022/10/12 10:06
    * @param phone
    * @param code
    */
    @Override
    public CommonResult loginByPhoneCode(String phone, String code,String codeMark) {
        Object o = redisUtil.get(phone + codeMark);
        if (code.equals(o)) {
            HashMap<Object, Object> map = new HashMap<>();
            UserProtalEntity userProtalEntity = new UserProtalEntity();
            userProtalEntity.setPhone(phone);
            UserProtalEntity userProtalEntity1 = userProtalMapper.selectOne(userProtalEntity);
            if (userProtalEntity1 != null) {
                String token = Base64Utils.encodeToString((phone+new Date()).getBytes());
                map.put("token", token);
                map.put("user", userProtalEntity1);
                return CommonResult.ok(map);
            } else {
                String token = Base64Utils.encodeToString((phone+new Date()).getBytes());
                userProtalEntity.setCreateTime(new Date());
                userProtalEntity.setDelFlag(0);
                userProtalEntity.setAvatarId(116);
                userProtalEntity.setRole(1);
                userProtalEntity.setAuditId(0);
                userProtalEntity.setNickName(RanNum.randomName(true,4));
                userProtalEntity.setUserName(RanNum.getItemID(9));
                int insert = userProtalMapper.insert(userProtalEntity);
                map.put("token", token);
                map.put("user", userProtalEntity);
                if (insert > 0) {
                    return CommonResult.ok(map);
                } else {
                    return CommonResult.error("500");
                }
            }
        } else {
            return CommonResult.error("1");
        }
    }
}
